
from django.shortcuts import render

# Create your views here.
<<<<<<< .merge_file_Jv4SDi
from rest_framework.generics import ListAPIView
=======
from requests import request
from rest_framework.generics import ListAPIView, CreateAPIView
>>>>>>> .merge_file_VwmMxi
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from rest_framework.views import APIView

from questions.models import Questions, Answer, Lable
from questions.serializer import QaSerializer, AnswerSerializer, LableSerializer, AnswerUserSerializer
from users.models import User


<<<<<<< .merge_file_Jv4SDi
class QaView(ListAPIView):
    queryset = Questions.objects.all()
    serializer_class = QaSerializer
    # permission_classes = [IsAuthenticated]
=======

>>>>>>> .merge_file_VwmMxi

def get_user_info(id):
    qs = User.objects.filter(id=id)
    x = AnswerUserSerializer(qs, many=True)
    return x


<<<<<<< .merge_file_Jv4SDi
class AnswerView(APIView):
    def get(self,request):

        lables_id = request.GET.get('lables')
        question_id = request.GET.get('question_id')
        pub_user_id = request.GET.get('pub_user_id')
        queryset = Answer.objects.filter(questions_id=question_id).order_by('answer_time')
        # 获取当前问题相关的回答用户信息和标签信息
        # 只需要最新一次数据
        serializer = AnswerSerializer(queryset,many=True)
        data = serializer.data
        # 提取answer_user_id
        answer_user_id = data[0]['answer_user_id']

        # print(answer_user_id)
        # 标签
        query_set = Lable.objects.filter(id=lables_id)
        s = LableSerializer(query_set,many=True)
        # print(s.data[0]['name'])
        # 将标签添加到data数据中传给前端
        data[-1]['name']=s.data[0]['name']

        # 回答者和发布者的名字
        # qs = User.objects.filter(id=answer_user_id)
        # x = AnswerUserSerializer(qs,many=True)
        x = get_user_info(answer_user_id)
        y = get_user_info(pub_user_id)
        # print(x.data[0]['username'])
        # 将回答问题用户名添加到data数据中传给前端
        data[-1]['answerusername'] = x.data[0]['username']

        data[-1]['pubusername'] = y.data[0]['username']
=======
# class AnswerView(APIView):
def get_answerinfo(element):

    lable = element['lable']
    question_id = element['id']
    pub_user_id = element['pub_user_id']

    queryset = Answer.objects.filter(questions_id=question_id).order_by('answer_time')
    # 获取当前问题相关的回答用户信息和标签信息
    # 只需要最新一次数据
    serializer = AnswerSerializer(queryset,many=True)
    data = serializer.data
    # 提取answer_user_id

    answer_user_id = data[0]['answer_user_id']
    element['answer_counts'] = data[0]['answer_counts']
    # print(answer_user_id)
    # 标签
    tags = []
    for id in lable:
        query_set = Lable.objects.filter(id=id)
        s = LableSerializer(query_set,many=True)
        # print(s.data[0]['name'])
        # 将标签添加到element数据中传给前端
        tag = s.data[0]['name']
        tags.append(tag)
    element['tags']= tags
    tags=[]

    # 回答者和发布者的名字
    # qs = User.objects.filter(id=answer_user_id)
    # x = AnswerUserSerializer(qs,many=True)
    x = get_user_info(answer_user_id)
    y = get_user_info(pub_user_id)
    # print(x.data[0]['username'])
    # 将回答问题用户名添加到data数据中传给前端
    element['answerusername'] = x.data[0]['username']

    element['pubusername'] = y.data[0]['username']


class QaView(APIView):
    def get(self, request):
        queryset = Questions.objects.all()

        serializer = QaSerializer(queryset, many=True)

        data = serializer.data

        for element in data:
            get_answerinfo(element)
>>>>>>> .merge_file_VwmMxi

        return Response(data)


<<<<<<< .merge_file_Jv4SDi
=======
class QuestionpubView(CreateAPIView):

    serializer_class = QaSerializer







>>>>>>> .merge_file_VwmMxi








